package com.tinysand.advert.controller;

import com.alibaba.fastjson.JSON;
import com.tinysand.advert.exception.AdvertException;
import com.tinysand.advert.service.AdUnitService;
import com.tinysand.advert.support.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 推广单元服务控制器
 *
 * Created By TINY
 * Create Time 2020/2/22
 **/
@Slf4j
@RestController
public class UnitController {

    private final AdUnitService adUnitService;

    public UnitController(AdUnitService adUnitService) {
        this.adUnitService = adUnitService;
    }

    @PostMapping("/unit/create")
    public AdUnitResponse createAdUnit(
            @RequestBody AdUnitRequest request) throws AdvertException {
        log.info("创建推广单元，请求参数（{}）", JSON.toJSONString(
                request
        ));

        return adUnitService.createAdUnit(request);
    }

    @PostMapping("/keyword/create")
    public AdKeywordResponse createAdKeyword(
            @RequestBody AdKeywordRequest request) throws AdvertException {
        log.info("创建关键词限制，请求参数（{}）", JSON.toJSONString(
                request
        ));

        return adUnitService.createUnitKeyword(request);
    }

    @PostMapping("/interest/create")
    public AdInterestResponse createAdInterest(
            @RequestBody AdInterestRequest request) throws AdvertException {
        log.info("创建兴趣限制，请求参数（{}）", JSON.toJSONString(
                request
        ));

        return adUnitService.createUnitInterest(request);
    }

    @PostMapping("/district/create")
    public AdDistrictResponse createAdDistrict(
            @RequestBody AdDistrictRequest request) throws AdvertException {
        log.info("创建地域限制，请求参数（{}）", JSON.toJSONString(
                request
        ));

        return adUnitService.createUnitDistrict(request);
    }

    @PostMapping("/rel/create")
    public AdUnitRelResponse createAdUnitRel(
            @RequestBody AdUnitRelRequest request) throws AdvertException {
        log.info("创建广告与推广单元的关系，请求参数（{}）", JSON.toJSONString(
                request
        ));

        return adUnitService.createAdUnitRel(request);
    }
}
